/*
 * Main.java
 *
 */
package da5221_lab1_2011;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;

/**
 *
 * @author Saeid Kazemi Kristianstad University January 2010
 */
public class Main {

    DiagonosArray sort;
    //declaration
    Scanner myScanner;
    long startTime, endTime;
    int[] sortMeHundredArr;
    int[] sortMeTenThousandArr;
    int[] sortMeMillionArr;
    int[] unSortedHundredArr;
    int[] unSortedTenThousandArr;
    int[] unSortedMillionArr;
    //You can have a currentArray variable here
    boolean firstTime;
    boolean sorted; //You can use the boolean firstTime instead.

    /**
     * Creates a new instance of Main
     */
    public Main() {

        //Initialization
        sort = new DiagonosArray();
        myScanner = new Scanner(System.in);

        sortMeHundredArr = new int[100];
        sortMeTenThousandArr = new int[10000];
        sortMeMillionArr = new int[1000000];

        unSortedHundredArr = new int[100];
        unSortedTenThousandArr = new int[10000];
        unSortedMillionArr = new int[1000000];

        firstTime = true;

        //The method fills the arrays with the numbers form  "milliion.txt""
        fillAllArrays();

        //The arrays are identical before the user sort them, after sorting they are different in the order of contents
    }//End of constructor

    public void myMainMethod() {
        int input, searchNumber;
        while (true) {
            printMenu();
            //Only accepts numbers in range of signed integers. It does not take care of entries other than those numbers.
            switch (myScanner.nextInt()) {
                case 1:
                    startTime = System.nanoTime();
                    sortMeHundredArr = sort.sortArray(sortMeHundredArr);
                    endTime = System.nanoTime();
                    System.out.println("Taken time for sorting sortMeHundredArr: " + (endTime - startTime));
                    startTime = System.nanoTime();

                    sortMeTenThousandArr = sort.sortArray(sortMeTenThousandArr);
                    endTime = System.nanoTime();
                    System.out.println("Taken time for sorting sortMeTenThousandArr: " + (endTime - startTime));

                    startTime = System.nanoTime();
                    sortMeMillionArr = sort.sortArray(sortMeMillionArr);
                    endTime = System.nanoTime();
                    System.out.println("Taken time for sorting sortMeMillionArr: " + (endTime - startTime));

                    //System.out.println("I'll write the code to sort three arrays which their names started with sortMe...");
                    //Write a class and some code here and sort the numbers ascending
                    // - sortMedArrHundredArr
                    // - sortMeTenThousandArr
                    // - sortMeMillionArrand
                    //Do not forget running time measurement sepaerately for each array
                    firstTime = false;
                    break;

                case 2:
                    //use "boolean sorted" in line 35 to prevent being activated before sorting
                    chooseArrayMenu();
                    System.out.println("1 for  minimum value of sorted array");
                    System.out.println("2  for  minimum value of unsorted array");
                    System.out.println("3  for minimum value of both array");
                    input = myScanner.nextInt();
                    if (input == 1 || input == 3) {
                        startTime = System.nanoTime();

                        System.out.println("Minimum value of sortMeHundredArr: " + sortMeHundredArr[0]);
                        endTime = System.nanoTime();

                        System.out.println("Taken time for minimum value of sortMeHundredArr:" + (endTime - startTime));
                        startTime = System.nanoTime();

                        System.out.println("Minimum value of sortMeTenThousandArr: " + sortMeTenThousandArr[0]);
                        endTime = System.nanoTime();

                        System.out.println("Taken time for minimum value of sortMeTenThousandArr: " + (endTime - startTime));
                        startTime = System.nanoTime();

                        System.out.println("Minimum value of sortMeMillionArr: " + sortMeMillionArr[0]);
                        endTime = System.nanoTime();

                        System.out.println("Taken time for minimum value of sortMeMillionArr: " + (endTime - startTime));

                    }
                    if (input == 2 || input == 3) {
                        startTime = System.nanoTime();

                        System.out.println("Minimum value of unsortMeHundredArr: " + sort.findMin(unSortedHundredArr));
                        endTime = System.nanoTime();

                        System.out.println("Taken time for Minimum value of unsortMeHundredArr:" + (endTime - startTime));
                        startTime = System.nanoTime();

                        System.out.println("Minimum value of unsortMeTenThousandArr: " + sort.findMin(unSortedTenThousandArr));
                        endTime = System.nanoTime();

                        System.out.println("Taken time for Minimum value of unsortMeTenThousandArr: " + (endTime - startTime));
                        startTime = System.nanoTime();

                        System.out.println("Minimum value of unsortMeMillionArr: " + sort.findMin(unSortedMillionArr));
                        endTime = System.nanoTime();

                        System.out.println("Taken time for Minimum value of unsortMeMillionArr: " + (endTime - startTime));



                    }
                    //System.out.println("I'll write the code to find the minimum in the chosen array ");
                    //write the code of finding minimum, better to implement method(s)/class(es)
                    //The user should have the option to choose any of the sortMe... or unsorted... arrays at a time
                    //Do not forget running time measurement
                    break;

                case 3:

                    //use "boolean sorted" in line 35 to prevent being activated before sorting
                    chooseArrayMenu();
                    System.out.println("Enter your number.");
                    searchNumber = myScanner.nextInt();
                    System.out.println("1 for search sorted array");
                    System.out.println("2  for  search unsorted array");
                    System.out.println("3  for search both array");
                    input = myScanner.nextInt();
                    if (input == 1 || input == 3) {
                        startTime = System.nanoTime();
                        System.out.println(searchNumber + " is happens in this sortMeHundredArr: " + sort.countNumber(sortMeHundredArr, searchNumber));
                        endTime = System.nanoTime();

                        System.out.println("Taken times: " + (endTime - startTime));


                        startTime = System.nanoTime();
                        System.out.println(searchNumber + " is happens in this sortMeTenThousandArr: " + sort.countNumber(sortMeTenThousandArr, searchNumber));
                        endTime = System.nanoTime();

                        System.out.println("Taken times: " + (endTime - startTime));


                        startTime = System.nanoTime();
                        System.out.println(searchNumber + " is happens in this sortMeMillionArr: " + sort.countNumber(sortMeMillionArr, searchNumber));
                        endTime = System.nanoTime();

                        System.out.println("Taken times: " + (endTime - startTime));


                        //System.out.println("Minimum value of sortMeTenThousandArr: "+sortMeTenThousandArr[0]);
                        //System.out.println("Minimum value of sortMeMillionArr: "+sortMeMillionArr[0]);
                    }
                    if (input == 2 || input == 3) {
                        startTime = System.nanoTime();
                        System.out.println(searchNumber + " is happens in this unsortMeHundredArr: " + sort.countNumber(unSortedHundredArr, searchNumber));
                        endTime = System.nanoTime();

                        System.out.println("Taken times: " + (endTime - startTime));


                        startTime = System.nanoTime();
                        System.out.println(searchNumber + " is happens in this unsortMeTenThousandArr: " + sort.countNumber(unSortedTenThousandArr, searchNumber));
                        endTime = System.nanoTime();

                        System.out.println("Taken times: " + (endTime - startTime));


                        startTime = System.nanoTime();
                        System.out.println(searchNumber + " is happens in this unsortMeMillionArr: " + sort.countNumber(unSortedMillionArr, searchNumber));
                        endTime = System.nanoTime();

                        System.out.println("Taken times: " + (endTime - startTime));



                        // System.out.println("Minimum value of unsortMeHundredArr: "+sort.findMin(unSortedHundredArr));
                        // System.out.println("Minimum value of unsortMeTenThousandArr: "+sort.findMin(unSortedTenThousandArr));
                        //System.out.println("Minimum value of unsortMeMillionArr: "+sort.findMin(unSortedMillionArr));
                    }
                    //System.out.println("I'll write the code to find how many times a specific number is repeated in the chosen array");
                    //Write the code of finding number of occurrences, better to implement method(s)/class(es)
                    //The user should have the option to choose any of the sortMe... or unsorted...  arrays at a time
                    //Do not forget running time measurement
                    break;

                case 4:
                    //use "boolean sorted" in line 36 to prevent being activated before sorting
                    chooseArrayMenu();
                    System.out.println("1 for sortMeHundredArr");

                    System.out.println("2 for sortMeTenThousandArr");
                    System.out.println("3 for  sortMeMillionArr");

                    System.out.println("4 for unsortMeHundredArr");
                    System.out.println("5 for unsortMeTenThousandArr");
                    System.out.println("6 for  unsortMeMillionArr");

                    input = myScanner.nextInt();
                    if (input == 1) {
                        printArray(sortMeHundredArr);
                    } else if (input == 2) {
                        printArray(sortMeTenThousandArr);
                    } else if (input == 3) {
                        printArray(sortMeMillionArr);
                    } else if (input == 4) {
                        printArray(unSortedHundredArr);
                    } else if (input == 5) {
                        printArray(unSortedTenThousandArr);
                    } else if (input == 6) {
                        printArray(unSortedMillionArr);
                    } else {
                        System.out.println("wrong input.");
                    }
                    // System.out.println("I'll write the code to print out the content of a chosen array");
                    // System.out.println("Array with one million integers may take long time to print out");
                    break;

                case 5:
                    System.out.println("Welcome back");
                    System.exit(0);
                    break;

                default:
                    System.out.println("The input is out of range");
                    break;
            } //End of switch
        } //End of while loop
    } //End of myMainMethod method

    private void printArray(int[] array) {
        for (int i : array) {
            System.out.print(i + " ");

        }


        //return null; //Null should be changed by you
    } //End of printArray method

    public static void main(String[] args) {
        //One easy way to get rid of static keyword in main method
        Main m1 = new Main();
        m1.myMainMethod();
    }

    private void fillAllArrays() {

        writeToArrayFromFile("million.txt", sortMeHundredArr);
        writeToArrayFromFile("million.txt", sortMeTenThousandArr);
        writeToArrayFromFile("million.txt", sortMeMillionArr);

        writeToArrayFromFile("million.txt", unSortedHundredArr);
        writeToArrayFromFile("million.txt", unSortedTenThousandArr);
        writeToArrayFromFile("million.txt", unSortedMillionArr);
    } //End of fillAllArrays method

    public void writeToArrayFromFile(String fileName, int[] inputArr) {
        int temp = inputArr.length;
        String s;
        try {
            BufferedReader b = new BufferedReader(new InputStreamReader(new FileInputStream(fileName)));
            while (temp > 0) {
                s = b.readLine();
                if (s == null) {
                    break;
                }
                inputArr[inputArr.length - temp--] = Integer.parseInt(s);
            }//End of try
            b.close();
        } catch (IOException ex) {
            System.out.println(ex.getMessage());
        }  //End of catch
    }//End of writeToArrayFromFile method

    private void chooseArrayMenu() {
        System.out.println("Enter a number between 1 to 6");

        System.out.println("1. Sorted array containing 100 integers");
        System.out.println("2. Sorted array containing 10,000 integers");
        System.out.println("3. Sorted array containing 1,000,000 integers");
        System.out.println("4. Unsorted array containing 100 integers");
        System.out.println("5. Unsorted array containing 10,000 integers");
        System.out.println("6. Unsorted array containing 1,000,000 integers");

    } //End of chooseArrayMenu

    private void printMenu() {
        if (firstTime) {
            System.out.println("\n\n***** Welcome to our application *****");
            System.out.println("******* Please follow the menu *******");
            System.out.println("\n**** Main menu ****");

            System.out.println("Select 1 or 5");
            System.out.println("1. Sort");
            System.out.println("2. (Unavailabe before sorting) Find minimum");
            System.out.println("3. (Unavailabe before sorting) Find number of occurrences");
            System.out.println("4. (Unavailabe before sorting) Print out one array");
            System.out.println("5. Quit");
        } //End of if
        else {
            System.out.println("\n**** Main menu ****");
            System.out.println("select a number between 1 to 5");
            System.out.println("1. Sort");
            System.out.println("2. Find minimum");
            System.out.println("3. Find number of occurrences");
            System.out.println("4. Print out one array");
            System.out.println("5. Quit");

        } //End of else
    } //End of printMenu method
}
